﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Librarian.Shared;

namespace Librarian.Pocket
{
    public partial class SearchForm : Form
    {
        public SearchForm()
        {
            InitializeComponent();
            SearchOptions = EzSearchOptions.All;
        }
        GridForm _form;
        
        protected override void OnClosing(CancelEventArgs e)
        {
            try
            {
                if (_form != null)
                    _form.Close();
            }
            finally
            {
                base.OnClosing(e);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                if (_form == null)
                    _form = new GridForm();


                _form.SearchOptions = SearchOptions;
                _form.FilterText = this.FilterText;
                _form.Find();
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
            _form.ShowDialog();
        }

        private void checkBoxTitle_CheckStateChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Title);
        }


        private void checkBoxAuthor_CheckStateChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Author);
        }

        private void checkBoxComments_CheckStateChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Comments);
        }

        private void checkBoxKeyword_CheckStateChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Keyword);
        }

        private void checkBoxLocation_CheckStateChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Location);
        }

        private void checkBoxTheme_CheckStateChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Theme);
        }

        private void checkBoxType_CheckStateChanged(object sender, EventArgs e)
        {
            toggle(EzSearchOptions.Type);
        }


        private void checkBoxAll_CheckStateChanged(object sender, EventArgs e)
        {
            if (!changing)
            {
                if (checkBoxAll.Checked)
                    SearchOptions = EzSearchOptions.None;
                else
                    SearchOptions = EzSearchOptions.All;
            }

            toggle(EzSearchOptions.All);
        }

        bool changing;
        public EzSearchOptions SearchOptions { get; set; }
        public string FilterText { get { return this.textBox1.Text; } }

        private void toggle(EzSearchOptions ezSearchOptions)
        {
            if (!changing)
            {
                changing = true;

                SearchOptions ^= ezSearchOptions;

                checkBoxAll.Checked = SearchOptions.HasValue(EzSearchOptions.All);
                checkBoxAuthor.Checked = SearchOptions.HasValue(EzSearchOptions.Author);
                checkBoxComments.Checked = SearchOptions.HasValue(EzSearchOptions.Comments);
                checkBoxKeyword.Checked = SearchOptions.HasValue(EzSearchOptions.Keyword);
                checkBoxLocation.Checked = SearchOptions.HasValue(EzSearchOptions.Location);
                checkBoxTheme.Checked = SearchOptions.HasValue(EzSearchOptions.Theme);
                checkBoxTitle.Checked = SearchOptions.HasValue(EzSearchOptions.Title);
                checkBoxType.Checked = SearchOptions.HasValue(EzSearchOptions.Type);

                changing = false;
            }
        }

    }
}